<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Backend for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Backend\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Service\ImportCsv\ImportSejourService as ImportSejourService;

class ImportController extends AbstractActionController
{
	protected $fileService;
	protected $importSejourService;

    public function creationAction() {

		return new ViewModel ();
    }

    public function uploadImageSejourAction() {
		if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
			$request = $this->getRequest();

			// $idSejour = $this->params()->fromPost('id'); 

			$files =  $request->getFiles()->toArray();

			$httpadapter = new \Zend\File\Transfer\Adapter\Http(); 
			$filesize  = new \Zend\Validator\File\Size(array('min' => 1));  
			$extension = new \Zend\Validator\File\Extension(array('extension' => array('csv')));

			$httpadapter->setValidators(array($filesize, $extension), $files['file']['name']);

			if($httpadapter->isValid()) {

				$oFileService = $this->getFileService();

				$dirPath = $oFileService->getRootPath();

				$httpadapter->setDestination( $dirPath );
				if($httpadapter->receive($files['file']['name'])) {
					
					$nomFichier = $files['file']['name'];
					$cheminComplet = $dirPath . $nomFichier;

					$ImportSejourService = $this->getImportSejourService();

					$ImportSejourService->setFile( $cheminComplet );

					$row = 1;
					if (($handle = fopen( $ImportSejourService->getFile(), "r")) !== FALSE) {
					    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {

					    	if($row == 1) {
					    		$ImportSejourService->verificationNomColonnes( $data );
					    	}

					        $num = count($data);
					        echo "<p> $num champs à la ligne $row: <br /></p>\n";
					        $row++;
					        for ($c=0; $c < $num; $c++) {
					            echo $data[$c] . "<br />\n";
					        }
					    }
					    fclose($handle);
					}

					$oFileService->supprimeImageFileSystem( $newfile );

					echo json_encode('Ok');
			 	}
			}
		}

		return $this->response;
	}

	/*************************************************************************************************************************/

	public function getFileService()
	{
		if( !$this->fileService ){
			$sm = $this->getServiceLocator();
			$this->fileService = $sm->get("FileService");
		}

		$this->fileService->setImportPath();

		return $this->fileService;
	}

	public function getImportSejourService()
	{
		if( !$this->importSejourService ){
			$sm = $this->getServiceLocator();
			$this->importSejourService = $sm->get("ImportSejourService");
		}

		return $this->importSejourService;
	}

}
